// 成本走势
var timeFormatMap = {
    'year': "%Y-%m",
    'month': "%Y-%m-%d",
    'day': "%Y-%m-%d %H",
    'hour': "%Y-%m-%d %H:%m"
};
var timeSingleFormatMap = {
    'year': "%m",
    'month': "%d",
    'day': "%H",
    'hour': "%i"
};

dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    var db = iot.db();
    var format,singleFormate;
    // var electricityResult = null;
    // var gasResult = null;
    // 2019之后的数据
    var carbonEq2019Result = null;

    // var timeSub = parseInt(theTime.substring(0,4));

    var resultData = {
        xData : iot.list(),
        yLeft: iot.list(),
        yRight : iot.list(),
        yName : iot.list()
    };

    var timeType = iot.getTimeByFormat(theTime);
    var timeDataList = getTimeList(theTime);
    format = timeFormatMap[timeType];
    singleFormate = timeSingleFormatMap[timeType];

    /*// 计划目标值
    var planResult;
    if (totalOrSingle.equals("total") && timeType == 'year') {
        planResult = db.qry("select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
            " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx, SUM(dev_win.dvalue) totalSum" +
            " from iot_dev_data_win dev_win, (select * from energy_struc_prop where type = 2 and field_type = '目标值' and energy_struc_id = "+ workSpace +") esp" +
            " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
            " and dev_win.the_time like '"+ theTime +"%' group by time");
    }*/

    // if(timeSub >= 2019){
        var fuSql = "select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
            " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
            " SUM(dev_win.dvalue) totalSum" +
            " from iot_dev_data_win dev_win, (select * from energy_struc_prop where type = 2 and field_type = '用量' and energy_struc_id = " + workSpace + ") esp" +
            " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
            " and dev_win.the_time like '" + theTime + "%' group by time";

        carbonEq2019Result = db.qry(fuSql);

    /*}else{
        //工厂  E001._用量*0.734 +  G001.用量*2.355
        if(frameworkIdToDevSn[factoryName] && frameworkIdToDevSn[factoryName][workSpace].ele){
            var doSql = "select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
                " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
                " SUM(dev_win.dvalue) totalSum" +
                " FROM iot_dev_data_win dev_win " +
                " LEFT JOIN iot_dev dev on dev.id=dev_win.dev_id " +
                " where 1=1 " +
                " and dev.sn='" + frameworkIdToDevSn[factoryName][workSpace].ele + "'" +
                " and dev_win.data_type = '_用量'" +
                " and dev_win.the_time like '" + theTime + "%' group by time";

            electricityResult = db.qry(doSql);
        }

        if(frameworkIdToDevSn[factoryName] && frameworkIdToDevSn[factoryName][workSpace].gas){
            gasResult = db.qry("select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
                " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
                " SUM(dev_win.dvalue) totalSum" +
                " FROM iot_dev_data_win dev_win " +
                " LEFT JOIN iot_dev dev on dev.id=dev_win.dev_id " +
                " where 1=1 " +
                " and dev.sn='"+frameworkIdToDevSn[factoryName][workSpace].gas+"'" +
                " -- and dev_win.data_type = '用量'\n" +
                " and dev_win.the_time like '"+ theTime +"%' group by time");
        }

    }*/

    var yieldId = getFactoryYieldId(factoryName)

    // 产量
    var yieldResult = db.qry("select DATE_FORMAT(the_time,'" + format + "') time," +
        " DATE_FORMAT(the_time,'" + singleFormate + "') timeIx," +
        " SUM(dvalue) totalSum from iot_dev_data_win" +
        " where " +
        " if("+ yieldId +" = 10687,data_type = '_PV产量' ,1=1)\n" +
        " and dev_id = "+ yieldId +" and the_time like '"+ theTime +"%' group by time");

    // 通过timeType获取当前时间
    var result = getCurrentTimeByType(timeType);
    var typeTime = result["typeTime"];
    var currTime = result["currTime"];

    // if(timeSub >= 2019){

        for(var ix in timeDataList){
            var timeItem = timeDataList[ix];
            var data  = 0;
            carbonEq2019Result.forAll(function (item,ix) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    data = parseInt(item.totalSum);
                }
            })

            var totalYield = 0;
            yieldResult.forAll(function (item) {
                if(item && item.timeIx && item.timeIx === timeItem){
                    totalYield = item.totalSum;
                }
            });
            //判断当前选择总数还是单车
            if (totalOrSingle.equals("total")) {
                resultData.xData.add(timeItem);
                // 把当前时间之后的数据干掉
                if (theTime == currTime && timeItem > typeTime) {
                    continue;
                }
                resultData.yLeft.add(data);
            }else if (totalOrSingle.equals("single")) {
                resultData.xData.add(timeItem);
                if (data && totalYield){
                    data = data/totalYield;
                    data = parseInt(data);
                }
                // 把当前时间之后的数据干掉
                if (theTime == currTime && timeItem > typeTime) {
                    continue;
                }
                if (totalYield === 0){
                    resultData.yLeft.add(0);
                }else{
                    resultData.yLeft.add(data);
                }
            }
        }

    /*}else{

        for(var ix in timeDataList){
            var timeItem = timeDataList[ix];
            var data  = 0;
            if (electricityResult) {
                electricityResult.forAll(function (item, ix) {
                    if (item && item.timeIx && item.timeIx === timeItem) {
                        data = item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].ele : 0);
                    }
                })
            }
            if (gasResult) {
                gasResult.forAll(function (item, ix) {
                    if (item && item.timeIx && item.timeIx === timeItem) {
                        data = parseInt(data + (item.totalSum * (carbonRowTimeModulus[factoryName] ? carbonRowTimeModulus[factoryName][timeSub].gas : 0)));
                    }
                })
            }

            var totalYield = 0;
            if (yieldResult) {
                yieldResult.forAll(function (item) {
                    if (item && item.timeIx && item.timeIx === timeItem) {
                        totalYield = item.totalSum;
                    }
                });
            }
            //判断当前选择总数还是单车
            if (totalOrSingle.equals("total")) {
                resultData.xData.add(timeItem);
                // 把当前时间之后的数据干掉
                if (theTime == currTime && timeItem > typeTime) {
                    continue;
                }
                resultData.yLeft.add(data);
            }else if (totalOrSingle.equals("single")) {
                resultData.xData.add(timeItem);
                // 把当前时间之后的数据干掉
                if (theTime == currTime && timeItem > typeTime) {
                    continue;
                }
                if (data && totalYield){
                    data = data/totalYield;
                    data = parseInt(data);
                }
                if (totalYield === 0){
                    resultData.yLeft.add(0);
                }else{
                    resultData.yLeft.add(data);
                }
            }
        }

    }*/

    // 添加计划数据
    for(var ix in timeDataList){
        var timeItem = timeDataList[ix];
        var data  = 0;
        /*if (planResult) {
            planResult.forAll(function (item) {
                if (item && item.timeIx && item.timeIx === timeItem) {
                    data = item.totalSum;
                }
            })
        }
        // 把当前时间之后的数据干掉
        if (theTime == currTime && timeItem > typeTime) {
            continue;
        }*/
        resultData.yRight.add(data);
    }
    // 添加两条y轴的名称，单位
    if (totalOrSingle.equals("total")) {
        resultData.yName.add('实际 (元)');
        resultData.yName.add('计划 (元)');
    } else {
        resultData.yName.add('实际 (元/辆)');
        resultData.yName.add('计划 (元/辆)');
    }

    resp.setJsData(resultData);
}